	/**
	 * 创建地图
	 * */
var bMap = {
	createMap:function(options){
		var defaults={'containerId' : null,//地图容器id
			 'lng' : null,//设置地图中心点
			 'lat': null,//设置地图中心点
			 'zoom': 5,//地图缩放级别默认为5
			 'minZoom':5,//设置地图允许的最小级别
			 'top_left_control': 'BMAP_ANCHOR_BOTTOM_LEFT',   //比例尺位置 默认为左下角 
							// BMAP_ANCHOR_TOP_LEFT	控件将定位到地图的左上角。
			                // BMAP_ANCHOR_TOP_RIGHT	控件将定位到地图的右上角。
			                // BMAP_ANCHOR_BOTTOM_LEFT	控件将定位到地图的左下角。
			                // BMAP_ANCHOR_BOTTOM_RIGHT
			 'top_left_controlshow':true,
			 'top_left_navigationshow':true
		};
		var opts = $.extend(defaults, options);  
		var map = new BMap.Map(opts.containerId,{minZoom:opts.minZoom});//初始化地图
		map.centerAndZoom(new BMap.Point(opts.lng,opts.lat), opts.zoom);
		var top_left_control = opts.top_left_controlshow ? new BMap.ScaleControl({anchor: opts.top_left_control}) : null;
		var top_left_navigation = opts.top_left_navigationshow ? new BMap.NavigationControl() : null;
		var ctrl = new BMapLib.TrafficControl({
           showPanel: true //是否显示路况提示面板
        });      
        map.addControl(ctrl);
        ctrl.setAnchor(BMAP_ANCHOR_BOTTOM_RIGHT); 
		map.addControl(new BMap.MapTypeControl({
			mapTypes: [BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]
		}));//添加地图类型控件
		map.addControl(top_left_control);//添加比例尺        
		map.addControl(top_left_navigation);//添加缩放平移控件     
		map.enableScrollWheelZoom();//启用滚轮放大缩小
		
		//自动定位到当前城市
		var myCity = new BMap.LocalCity();
		myCity.get(function(result){
			var cityName = result.name;
			map.setCenter(cityName);
			map.setZoom(opts.zoom);
		});
		return map;
	},
	/**
	 * 设置围栏
	 * 传入当前地图对象map
	 * options{ 
	 *   fenceType:BMAP_DRAWING_CIRCLE, //默认为画圆
	 *   func:null,//回调函数 绘画完成调用此方法
	 *   enableDrawingTool:false 是否显示绘画工具栏 默认为不显示
	 * }
	 * BMAP_DRAWING_MARKER 画点
	 * BMAP_DRAWING_CIRCLE 画圆
	 * BMAP_DRAWING_POLYLINE 画线
	 * BMAP_DRAWING_POLYGON 画多边形
	 * BMAP_DRAWING_RECTANGLE 画矩形
	 * 并返回围栏对象
	 * */
	setFence:function(map,options){
		var myDrawingManagerObject = new BMapLib.DrawingManager(map,{
				isOpen: true, 
				enableDrawingTool: options.enableDrawingTool
	    });
		var funtype = 'circlecomplete';
		if(options.fenceType == BMAP_DRAWING_MARKER){
			funtype = 'markercomplete';
		}
		if(options.fenceType == BMAP_DRAWING_POLYLINE){
			funtype = 'polylinecomplete';
		}
		if(options.fenceType == BMAP_DRAWING_POLYGON){
			funtype = 'polygoncomplete';
		}
		if(options.fenceType == BMAP_DRAWING_RECTANGLE){
			funtype = 'rectanglecomplete';
		}
	    myDrawingManagerObject.setDrawingMode(options.fenceType);
	    myDrawingManagerObject.addEventListener(funtype, options.func);
	    return myDrawingManagerObject;
	},
	/**
	 * 创建标注
	 * options{
	 *     iconimg:null,标注图片路径
	 *     width:null,图片宽度
	 *     height:null,图片高度
	 *     lng:null,标注位置点
	 *     lat:null
	 * }
	 * return 标注对象
	 * */
	setMarker:function(options){
		 var p1 = new BMap.Point(options.lng,options.lat);
		 var icon1 = new BMap.Icon(options.iconimg, new BMap.Size(options.width,options.height));
		 var m1 = new BMap.Marker(p1,{icon:icon1}); 
		 return m1;
	},
	/**
	 * 打开测距控件
	 * */
	openRanging:function(map){
		 var myDis = new BMapLib.DistanceTool(map);
		 myDis.open();
	},
	/**
	 * 打开实时路况
	 * 显示当天的12个小时里的路况信息 
	 * 
	 * */ 
	openTraffic:function(map){
		 return ctrl;
	},	
	/**
	 *区域规划
	 * */
	pStart:null,
	map:null,
	rectangle:null,
	areaProgramme:function(map){
		bMap.map = map;
		bMap.map.disableDragging();
		bMap.map.addEventListener("click",bMap.areaClickLis);
		bMap.map.addEventListener("rightclick",bMap.areaRightLis);
	},
	areaRightLis:function(e){
		bMap.map.removeEventListener("click",bMap.areaClickLis);
		bMap.map.removeEventListener("mousemove",bMap.areaMouseMoveList);
		bMap.map.removeEventListener("rightclick",bMap.areaRightLis);
		bMap.map.enableDragging();
		var iw = null;
		bMap.rectangle.addEventListener("mouseover",function(e){
			var _html = '<a><img src="../img/cancel-circle.png" width: 16px; height: 16px; /></a>';
			if(iw == null){
				iw = new BMap.Label(_html,{"offset":new BMap.Size(13,-20),position:e.point});
				bMap.map.addOverlay(iw);
			}else{
				iw.setContent(_html);
				iw.setPosition(e.point);
				bMap.map.addOverlay(iw);
			}
		});
		bMap.rectangle.addEventListener("mouseout",function(e){
			bMap.map.removeOverlay(iw);
		});
	},
	areaClickLis:function(e){
		bMap.pStart = e.point;
		bMap.map.addEventListener("mousemove",bMap.areaMouseMoveList);
	},
	areaMouseMoveList:function(mm){
		var pEnd = mm.point;
		bMap.map.removeOverlay(bMap.rectangle);
		bMap.rectangle = new BMap.Polygon([
			new BMap.Point(bMap.pStart.lng,bMap.pStart.lat),
			new BMap.Point(pEnd.lng,bMap.pStart.lat),
			new BMap.Point(pEnd.lng,pEnd.lat),
			new BMap.Point(bMap.pStart.lng,pEnd.lat)
		], {strokeColor:"red", strokeWeight:2, strokeOpacity:0.5}); 
		bMap.map.addOverlay(bMap.rectangle);
	},
	 /**
     * 根据经纬度获取地址
     * callback 成功回调函数,不能为空
     * */
    getLocationByGps:function(gpspoint,callback){
    	 var geoc = new BMap.Geocoder();
         geoc.getLocation(gpspoint, function (rs) {
             var addComp = rs.addressComponents;
             var addr = addComp.province;
             if (addr == addComp.city) {
                 addr = addComp.city;
             } else {
                 addr += addComp.city 
             }
             addr+= addComp.district + addComp.street + addComp.streetNumber;
             if(callback){
            	 callback(addr);
             }
         });
    },
	/**
	 * 路线规划
	 * */
	pathProgramme:function(map,options){
		var defaults = {
				"enable":true,
				"panel":null,
				"searchComplete":null
		};
		var opts = $.extend(defaults, options);  
		var transit = new BMap.DrivingRoute(map, {
			renderOptions: {
				map: map,
				panel: opts.panel,//是否将线路结果呈现
				enableDragging : opts.enable, //起终点可进行拖拽
			},
			onSearchComplete:opts.searchComplete//规划完成回调
		});
		transit.clearResults();
		transit.search(opts.start,opts.end,options.waypoints);
	},
	/**x
	 * 地图关键字搜索
	 * 需一个输入框id
	 * 一个隐藏的div:用于显示关键字搜索的多条信息
	 * options{
	 * map:当前的地图对象，
	 * inputId：输入框id
	 * divId:隐藏的divid
	 * }
	 * 
	 * */
	automaticSearch:function(options){
		var myCity = new BMap.LocalCity();
		myCity.get(function(result){
			var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
				{"input" : options.inputId
				,"location" :options.map
			});

			ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
				var str = "";
				var _value = e.fromitem.value;
				var value = "";
				if (e.fromitem.index > -1) {
					value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				}    
				str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
				
				value = "";
				if (e.toitem.index > -1) {
					_value = e.toitem.value;
					value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				}    
				str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
				$("#"+options.divId).innerHTML = str;
			});

			var myValue;
			ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
				var _value = e.item.value;
				myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				$("#"+options.divId).innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
				
				bMap.setPlace(options.map,myValue);
			});
		});
	},
	setPlace:function(options){
		var map = options.map;
		var laceMarker=null;
		$("#"+options.inputId).keyup(function(){
			var myValue = $("#"+options.inputId).val();
			if(''==myValue){
				$("#as-results-099").remove();
				return;
			}
			var local = new BMap.LocalSearch($("#val").html(), { //智能搜索
				pageCapacity :30,
				forceLocal:true,
				onSearchComplete: function(results){
					if (local.getStatus() == BMAP_STATUS_SUCCESS){
						var s = [];
						var _html = '<div id="as-results-099"  style="display: block; z-index: 999; position: absolute; left: 545px;top:180px;" >'
									+'<ul class="as-list" style="height: 400px; overflow: auto; width: 412px;">';
						for (var i = 0; i < results.getCurrentNumPois(); i ++){
							_html+='<li id="as-result-item-0" class="as-result-item" poi="'+results.getPoi(i).point.lng+','+results.getPoi(i).point.lat+'" >'
								+results.getPoi(i).title+'  '+(results.getPoi(i).address==undefined?"":results.getPoi(i).address)+"</li>";
						}
						_html += "</ul></div>";
						$("#as-results-099").remove();
						$("body").append(_html);
						$("#as-results-099 ul li").hover(
							function(){
								$(this).addClass("active").siblings().removeClass("active");
							},
							function(){
								$(this).removeClass("active").siblings().removeClass("active");
							}
						).click(function(){
							$("#as-results-099").hide();
							map.removeOverlay(laceMarker);    //清除地图上所有覆盖物
							var pp = new BMap.Point($(this).attr("poi").split(",")[0],$(this).attr("poi").split(",")[1]);    //获取第一个智能搜索的结果
							map.centerAndZoom(pp, 18);
							laceMarker = new BMap.Marker(pp);
							map.addOverlay(laceMarker);    //添加标注
							$("#"+options.inputId).val($(this).html().split('  ')[1]);
						});
					}else{
						map.removeOverlay(laceMarker);    
						$("#as-results-099").remove();
					}
				}
			});
			local.search(myValue);
		});	
	},
	/***
	 * 本地搜到poi点
	 * 参数:opts{pageSize:20,keyWords:'aa',range:3000,city:'深圳市',cityCenter:{lat:23,lng:123}} 
	 * callback回调
	 */
	searchPoi : function(opts,callback){
		var options = {
			pageCapacity : opts.pageSize || 20,//页面显示大小
			forceLocal:true,//当前地图范围搜索
			onSearchComplete: function(results){//成功回调
			    // 判断状态是否正确
			    if (local.getStatus() == BMAP_STATUS_SUCCESS){
				    var _data = [];
				    for (var i = 0; i < results.getCurrentNumPois(); i ++){
				    	var ret = results.getPoi(i);
				    	var unit = {
				    			title : ret.title,
				    			province : ret.province,
				    			city : ret.city,
				    			address : ret.address,
				    			phoneNumber : ret.phoneNumber,
				    			point : ret.point
				    	};
				    	_data.push(unit);
				    }
				    
			    }
			    if($.isFunction(callback)){
					callback(_data);
				}
			}
		};
		var local = new BMap.LocalSearch(opts.city, options);
		local.searchNearby(opts.keyWords,new BMap.Point(opts.cityCenter.lng,opts.cityCenter.lat),opts.range);
	}
};